// pages/management/indent/index.js
const netWorked = require('../../../utils/netWork.js')
const Config = require('../../../utils/confing.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show:false,//预约弹出框
    date: '2015-01-01',//默认起始时间  
    date2: '2022-01-01',//默认结束时间 
    data:[],
    rideoid:"",
    imgshow: false,
    addshow: false,
    shangjiashow:false,
    imgdata:[],
    imgs:[],
    addimgs:[],
    pageindex:1,

    yuyueid: "",
    loginid:'',
    branch_no: '',


    item_name: "",
    memo: "",
    item_barcode: "",
    sale_price: "",

    shangjiaid:"",





  },
  input1(e) {
    let item_name = e.detail
    this.setData({
      item_name
    })
  },

  shangjiain(e){
    let shangjiaid = e.detail
    this.setData({
      shangjiaid
    })
  },
  input2(e) {
    let memo = e.detail
    this.setData({
      memo
    })
  },
  input3(e) {
    let item_barcode = e.detail
    this.setData({
      item_barcode
    })
  },
  input4(e) {
    let sale_price = e.detail
    this.setData({
      sale_price
    })
  },
  //新增 
  addok(){
    let _this = this;
    let data = this.data.addimgs;

    if (data.length==0){
      wx.showToast({
        title: '请上传图片',
        icon: "none",
      })
      return;
    }

    for (var i = 0, h = data.length; i < h; i++) {
      wx.uploadFile({
        url: Config.BaseUrl + 'ImgManger/ItemImageAdd',
        filePath: data[i],
        name: 'upload',
        formData: {
          'order_id': _tihs.data.loginid
        },
        header: {
          "Content-Type": "multipart/form-data"
        },
        success: function (res) {
          console.log(res)
          wx.showToast({
            title: '上传成功',
            icon: "none",
          })
          _this.setData({
            addshow: false,
        
          })
          //刷新页面
          const pages = getCurrentPages()
          const perpage = pages[pages.length - 1]
          perpage.onLoad()  
          // console.log(res)
        },
        fail: function (res) {
          wx.hideToast();
          wx.showModal({
            title: '错误提示',
            content: '上传图片失败',
            showCancel: false,
            success: function (res) { }
          })
        }
      });
    }

    if (data.length>0){
      wx.request({
        url: Config.BaseUrl + 'ImgManger/add',
        method: "POST",
        data: {
          item_name: _this.data.item_name,
          memo: _this.data.memo,
          item_barcode: _this.data.item_barcode,
          sale_price: _this.data.sale_price,
          order_id: _this.data.loginid,
        },
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function (res) {
          console.log(res.data)
          let data = res.data
          _this.setData({
            addimgs:[],

          })
        }
      })

    }
    


  },
  //上架
  shangjiaok(){
    let _this=this
    let where = "item_no='" + _this.data.shangjiaid+"'";
    wx.request({
      url: Config.BaseUrl + 'WxApplets/SelCommodity?where=' + where ,
      method: "POST",
      success: function (res) {

        if (res.data != null) {
          wx.request({
            url: Config.BaseUrl + 'ImgManger/UpperShelf?id=' + _this.data.radioid + '&&id2=' + _this.data.shangjiaid,
            method: "POST",
            success: function (res) {
              console.log(res.data)
              let data = res.data
              if (data!=null){
                wx.showToast({
                  title: '上架成功',
                  icon: "none",
                })
              }
              _this.setData({
                shangjiashow: false,
                shangjiaid:""
                })
 
              //刷新页面
              const pages = getCurrentPages()
              const perpage = pages[pages.length - 1]
              perpage.onLoad()  
            }
          })
        }else{
          wx.showToast({
            title: '商品编号不正确',
            icon: "none",
          })
        }

      }
    })
  },
  // 关闭上架页面
  shangjiahide: function (e) {

    this.setData({
      shangjiashow: false,
    })
  },
  // 打开上架页面
  shangjiashow: function(e) {
    let _this = this
    if (_this.data.radioid == '' || _this.data.radioid == undefined) {
      wx.showToast({
        title: '请选择',
        icon: "none",
      })
      return;
    }
    this.setData({
      shangjiashow: true,
    })

  
  },
  // 关闭预约页面
  hide: function (e) {
    this.setData({
      show: false,
    })
  },
  //点击单选框
  radio(e){
    
    this.setData({
      radioid: e.detail.value,
    })

  },
  // 打开新增页面
  addshow: function (e) {
    this.setData({
      addshow: true,

    })
  },
  // 关闭新增页面
  addhide: function (e) {
    this.setData({
      addshow: false,

    })
  },

  // 打开预约页面
  show: function (e) {
    console.log(e.currentTarget.dataset.id)
    this.setData({
      yuyueid: e.currentTarget.dataset.id,
      show: true,
   
    })
  },
  // 关闭图片页面
  imghide: function (e) {
    this.setData({
      imgshow: false,

    })
  },
  // 打开图片页面
  imgshow: function (e) {
    let _this = this
    if (_this.data.radioid == '' || _this.data.radioid == undefined){
      wx.showToast({
        title: '请选择',
        icon: "none",
      })
    return;
   }
   
    wx.request({
      url: Config.BaseUrl + 'Archives/SelItemImg?id=' + _this.data.radioid,
      method: "POST",
      success: function (res) {
        let data = res.data
        _this.setData({
          imgshow: true,
          show: false,
          imgs: [],
          imgdata: res.data,
        })
      }
    })
  },

  //上传图片
  chooseImage(e) {
    let that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],  //可选择原图或压缩后的图片
      sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
      success: res => {
        // const images = this.data.images.concat(res.tempFilePaths)
        let img = this.data.imgs;
        this.data.imgs = this.data.imgs.concat(res.tempFilePaths);
        that.setData({
          imgs: this.data.imgs
        })
      }
    })
  },

  //新增上传图片
  addImage(e) {
    let that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],  //可选择原图或压缩后的图片
      sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
      success: res => {
        // const images = this.data.images.concat(res.tempFilePaths)
        let img = this.data.imgs;
        this.data.imgs = this.data.imgs.concat(res.tempFilePaths);
        that.setData({
          addimgs: this.data.imgs
        })
      }
    })
  },

  //图片删除 
  detImg(e) {
    let that = this
    let index = e.currentTarget.dataset.index;
    let imgid = e.currentTarget.dataset.imgid;

    wx.showModal({
      title: '提示',
      content: '确定要删除嘛？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: Config.BaseUrl + '/Archives/DelItemImg?id=' + imgid,
            method: "POST",
            success: function (res) {
              // console.log(res.data)

              wx.showToast({
                title: '删除成功',
                icon: "none",
              })

              let goods = that.data.imgdata

              goods.splice(index, 1);
              that.setData({
                imgdata: goods
              })

            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    // console.log(goods)
  },

  //图片删除 
  clearImg(e) {
    let that = this
    let index = e.currentTarget.dataset.index;
    let goods = this.data.imgs
    goods.splice(index, 1);
    // console.log(imgss)
    that.setData({
      imgs: goods
    })

    // console.log(goods)
  },
  //新增图片删除 
  clearaddImg(e) {
    let that = this
    let index = e.currentTarget.dataset.index;
    let goods = this.data.imgs
    goods.splice(index, 1);
    // console.log(imgss)
    that.setData({
      addimgs: goods
    })

    // console.log(goods)
  },

  //保存图片
  tijiao() {
    let _this = this;
    let data = this.data.imgs;
    console.log(this.data.radioid)
    for (var i = 0, h = data.length; i < h; i++) {
   
      wx.uploadFile({
        url: Config.BaseUrl + 'ImgManger/PreUploadImage',
        filePath: data[i],
        name: 'upload',
        formData: {
          'item_no': _this.data.radioid
        },
        header: {
          "Content-Type": "multipart/form-data"
        },
        success: function (res) {
          wx.request({
            url: Config.BaseUrl + 'Archives/SelItemImg?id=' + _this.data.radioid,
            method: "POST",
            success: function (res) {
              console.log(res.data)
              let data = res.data
              _this.setData({
                imgdata: res.data,

              })
            }
          })
          wx.showToast({
            title: '上传成功',
            icon: "none",
          })
          // console.log(res)
        },
        fail: function (res) {
          wx.hideToast();
          wx.showModal({
            title: '错误提示',
            content: '上传图片失败',
            showCancel: false,
            success: function (res) { }
          })
        }
      });
    }

    if (this.data.imgs.length == 0) {
      _this.setData({
        imgshow: false,
      })
    }
    _this.setData({
      imgs: [],
    })

  },
  //预约
  yuyueok(){

    let _this=this
    wx.request({
      url: Config.BaseUrl + 'ImgManger/Yuyue',
      data: {
        num: 10,
        N_item_no: _this.data.yuyueid,
        customer_info: _this.data.loginid,
        branch_no: _this.data.branch_no
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      success: function (res) {
        console.log(res.data.insert)
        if (res.data.insert>0){
          wx.showToast({
            title: '预约成功',
            icon: "none",
          })
          
          _this.setData({
            show: false,
          })
        }else{
          wx.showToast({
            title: '预约失败',
            icon: "none",
          })
        }
         
      
      }
    })

  },

  //设置封面
  handleLongPress(e) {
    let _tihs = this
    wx.request({
      url: Config.BaseUrl + 'Archives/UpdIMgCover?id=' + _tihs.data.radioid + '&imgid=' + e.currentTarget.dataset.imginde,
      method: "POST",
      success: function (res) {

        wx.request({
          url: Config.BaseUrl + 'Archives/SelItemImg?id=' + _tihs.data.radioid,
          method: "POST",
          success: function (res) {
            let data = res.data
            _tihs.setData({
              imgshow: true,
              imgdata: res.data,
            })
          }
        })

        wx.showToast({
          title: '设置封面成功',
          icon: "none",
        })

      }
    })
  },

  //下架
  delete(event) {
    let id = this.data.radioid;
    if (this.data.radioid == '' || this.data.radioid == undefined) {
      wx.showToast({
        title: '请选择',
        icon: "none",
      })
      return;
    }
    wx.showModal({
      title: '提示',
      content: '确定要下架['+id+']嘛？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: Config.BaseUrl + 'ImgManger/LowerShelf?id=' + id,
            method: "POST",
            success: function (res) {
              // console.log(res.data)

              if (res==1){
                wx.showToast({
                  title: '下架成功',
                  icon: "none",
                })
              }else{
                wx.showToast({
                  title: '下架失败',
                  icon: "none",
                })
              }
             


              //刷新页面
              const pages = getCurrentPages()
              const perpage = pages[pages.length - 1]
              perpage.onLoad()

            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  // 时间
  bindDateChange(e) {
    let that = this;
    that.setData({
      date: e.detail.value,
    })
    let where = "CreateTime between '" + that.data.date + " 00:00:00' and '" + that.data.date2 + " 23:59:59'";

    var _this = this;
    netWorked.netWork({
      url: '/ImgManger/loadImg?pageindex=1&where=' + where,
      method: "POST",
      success: function (res) {
        
        _this.setData({
          data: res.data.tabledata,
          pageindex: 1
        })
      }
    })



  },
  bindDateChange2(e) {
    let that = this;
    that.setData({
      date2: e.detail.value,
    })

    let where = "CreateTime between '" + that.data.date + " 00:00:00' and '" + that.data.date2 + " 23:59:59'";

    var _this = this;
    netWorked.netWork({
      url: '/ImgManger/loadImg?pageindex=1&where=' + where,
      method: "POST",
      success: function (res) {

        _this.setData({
          data: res.data.tabledata,
          pageindex: 1
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {


   
    let where="1=1";
    var _this = this;


    _this.setData({
      loginid: wx.getStorageSync("loginid"),
      branch_no: wx.getStorageSync("branch_no"),
    })
    netWorked.netWork({
      url: '/ImgManger/loadImg?pageindex=1&where=' + where,
      method: "POST",
      success: function (res) {
        _this.setData({
          data: res.data.tabledata,
          pageindex: 1
        })
      }
    })
  },
  onSearch(e){
    let that=this
    let where = "(CreateTime between '" + that.data.date + " 00:00:00' and '" + that.data.date2 + " 23:59:59') and id like '" + e.detail + "%25' or name like '%25" + e.detail + "%25' or  barcode like '%25" + e.detail + "%25'";
    netWorked.netWork({
      url: '/ImgManger/loadImg?pageindex=1&where=' + where,
      method: "POST",
      success: function (res) {
        
        that.setData({
          data: res.data.tabledata,
          pageindex: 1
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let where = "1=1";
    var _this = this;
   
  

    netWorked.netWork({
      url: '/ImgManger/loadImg?pageindex=1&where=' + where,
      method: "POST",
      success: function (res) {
        _this.setData({
          data: res.data.tabledata,
          pageindex:1
        })
      }
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let where = "1=1";
    var _this = this;
    wx.request({
      url: Config.BaseUrl +'/ImgManger/loadImg?pageindex=' + (_this.data.pageindex+1) + '&where=' + where,
      method: "POST",
      success: function (res) {
        if (res.data.tabledata!=null) {
        _this.setData({
          data: _this.data.data.concat(res.data.tabledata),
          pageindex: _this.data.pageindex + 1,
        })
        }
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})